﻿using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml.Serialization;
using WeiBeeCommon.Core;
using WeiBeeCommon.DataTypes;

/// <summary>
/// Summary description for WebUser
/// </summary>
public class WebUser
{
	public WebUser()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public string Username;
    public string Pwd;
    public string QQToken;
    public string QQSecret;
    public string SinaToken;
    public string SinaSecret;
    public string SohuToken;
    public string SohuSecret;
}
public class WebUserHelper
{
     List<WebUser> _users;
     public WebUserHelper(string store)
     {
         _entityfile = store;
         _users = LoadAccounts();
     }
     private readonly string _entityfile = "webusers.xml";
     private List<WebUser> LoadAccounts()
     {
         var result = new List<WebUser>();
         if (File.Exists(_entityfile))
         {
             using (var fs = new FileStream(_entityfile, FileMode.Open, FileAccess.Read))
             {
                 var root = new XmlRootAttribute("Accounts");
                 var ser = new XmlSerializer(typeof(List<WebUser>), root);
                 result = ser.Deserialize(fs) as List<WebUser>;
                 fs.Close();
             }
         }
         return result;
     }

     public void SaveAccounts(List<WebUser> entities)
     {
         using (var fs = new FileStream(_entityfile, FileMode.Create, FileAccess.Write))
         {
             var root = new XmlRootAttribute("Accounts");
             var ser = new XmlSerializer(typeof(List<WebUser>), root);
             ser.Serialize(fs, entities);
             fs.Close();
         }
     }

     public bool IsUserValid(string username, string pwd)
     {
         return _users.Exists(u => string.Compare(username, u.Username) == 0 && string.Compare(pwd, u.Pwd) == 0);
     }
     public void AddAccount(string user, string pwd)
     {
         // To do: check user already exists
         var acc = new WebUser();
         acc.Username = user.Trim();
         acc.Pwd = pwd.Trim();
         _users.Add(acc);
         SaveAccounts(_users);
     }

     public void UpdateWeiBeeInfo(string u, IWeiBee weibee)
     {
         var webuser = _users.Where(user => string.Compare(user.Username,u)==0).First();
         switch (weibee.UserType)
         {
             case WeiBeeType.QQ:
                 webuser.QQToken = weibee.GetOAuth().Token;
                 webuser.QQSecret = weibee.GetOAuth().TokenSecret;
                 break;
             case WeiBeeType.Sina:
                 webuser.SinaToken = weibee.GetOAuth().Token;
                 webuser.SinaSecret = weibee.GetOAuth().TokenSecret;
                 break;
             case WeiBeeType.Sohu:
                 webuser.SohuToken = weibee.GetOAuth().Token;
                 webuser.SohuSecret = weibee.GetOAuth().TokenSecret;
                 break;
             default:
                 break;
         }
         SaveAccounts(_users);
     }

     public List<WeiBeeAccount> GetUserTokens(string p)
     {
         var result = new List<WeiBeeAccount>();
         var user = _users.Where( u => string.Compare(u.Username,p)==0).First();
         if (!string.IsNullOrEmpty(user.QQToken))
         {
             var qq = new WeiBeeAccount();
             qq.UserType = WeiBeeType.QQ;
             qq.Token = user.QQToken;
             qq.TokenSecret = user.QQSecret;
             result.Add(qq);
         }
         if (!string.IsNullOrEmpty(user.SohuToken))
         {
             var sohu = new WeiBeeAccount();
             sohu.UserType = WeiBeeType.Sohu;
             sohu.Token = user.SohuToken;
             sohu.TokenSecret = user.SohuSecret;
             result.Add(sohu);
         }
         if (!string.IsNullOrEmpty(user.SinaToken))
         {
             var sina = new WeiBeeAccount();
             sina.UserType = WeiBeeType.Sina;
             sina.Token = user.SinaToken;
             sina.TokenSecret = user.SinaSecret;
             result.Add(sina);
         }
         //using (System.IO.StreamWriter writer = new System.IO.StreamWriter(@"D:\WebPages\Cachecallback.txt", true))
         //{
         //    writer.WriteLine("Total:{0}",result.Count);
         //}
         return result;
     }
}
